package com.example.testrxjava.login;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.testrxjava.IndexActivity;
import com.example.testrxjava.MainActivity;
import com.example.testrxjava.R;
import com.example.testrxjava.base.BaseActivity;
import com.example.testrxjava.register.RegisterActivity;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.CommUtil;
import com.example.testrxjava.util.SpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.loginUsername)
    EditText loginUsername;
    @BindView(R.id.loginPassword)
    EditText loginPassword;
    @BindView(R.id.loginCommit)
    TextView loginCommit;
    @BindView(R.id.goRegister)
    TextView goRegister;
    private final String TAG="LoginActivity";

    private String userName;
    private String passWord;


    private Map<String,String> map;
    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    private void saveUserInfo(){
        userName=loginUsername.getText().toString().trim();
        passWord=loginPassword.getText().toString().trim();
        map.put("username",userName);
        map.put("password",passWord);
    }


    @Override
    public void init(Bundle savedInstanceState) {
            map=new HashMap<String,String>();
            initLinstener();
    }

    @Override
    public void initLinstener() {
        loginCommit.setOnClickListener(this);
        goRegister.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.loginCommit:
                saveUserInfo();
                if(TextUtils.isEmpty(userName)&&TextUtils.isEmpty(passWord)){
                    CommUtil.Toast(LoginActivity.this,"输入不能为空！");
                    return;
                }

                Action.getInstance().login(this,map, new LoginSucessCallBack() {
                    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void sucess(LoginBean bean) {
                        Log.e(TAG,bean.getData().getUsername());
                        SpUtils.saveLoginInfo(LoginActivity.this,userName,passWord);
//                        CommUtil.startActivity(LoginActivity.this, MainActivity.class);
                        Intent intent=new Intent(LoginActivity.this, IndexActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void fail(String message) {
                        CommUtil.Toast(LoginActivity.this,message);
                    }
                });

                break;
            case R.id.goRegister:
                Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
               startActivity(intent);
               finish();
                break;
        }

    }
}
